﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FBidders : FormBase
    {
        private EDM_AuctionContainer context;
        Bidder bidder = null;
        Validations valid = new Validations();
        public enum ACTION { VIEW, SEARCH, MODIFY, CREATE, ADDBOND, DELETE };
        ACTION actionActual = ACTION.VIEW;


        public FBidders(ACTION anAction)
        {
            InitializeComponent();
            actionActual = anAction;
        }

        public FBidders(ACTION anAction, EDM_AuctionContainer context)
        {
            InitializeComponent();
            actionActual = anAction;
            this.context = context;
        }

        private void FBidders_Load(object sender, EventArgs e)
        {
            PreparePanel();
        }

        private void PreparePanel()
        {
            if (actionActual == ACTION.VIEW)
            {
                b_save.Visible = false;
                b_createVirtualAccount.Visible = false;
                pnlSearchClient.Visible = false; 
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.StartPosition = FormStartPosition.CenterParent;
               
                this.ControlesHabilitados(false);
            }
            if (actionActual == ACTION.SEARCH)
            {
                b_save.Visible = false;
                b_createVirtualAccount.Visible = false;   
                pnlSearchClient.Visible = true; 
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }

            if (actionActual == ACTION.DELETE)
            {
                b_save.Visible = false;
                b_createVirtualAccount.Visible = false;
                pnlSearchClient.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }
            else if (actionActual == ACTION.MODIFY)
            {
                b_save.Visible = true;
                b_createVirtualAccount.Visible = true;
                this.ControlesHabilitados(false);
            }

            else if (actionActual == ACTION.ADDBOND)
            {
                b_save.Visible = true;
                this.ControlesHabilitados(false);
            }

            else if (actionActual == ACTION.CREATE)
            {
                b_save.Visible = true;
                b_createVirtualAccount.Visible = true;
                this.ControlesHabilitados(true);
            }
            if (actionActual == ACTION.SEARCH || actionActual == ACTION.MODIFY || actionActual==ACTION.ADDBOND ||actionActual==ACTION.DELETE)
            {
                FSearchClient f_SearchClient = new FSearchClient(this, context); 
                f_SearchClient.TopLevel = false;
                 this.pnlSearchClient.Controls.Add(f_SearchClient);
                 f_SearchClient.Show();
            }
        }

        public override void TransferBidderFound(Bidder aBidder)
        {
            this.bidder = aBidder;
            b_save.Enabled = true;
            pnlSearchClient.Visible = false;
            LoadDataOfBidder(bidder);
            if (actionActual == ACTION.ADDBOND)
            {
                t_addBond.Visible = true;
                t_addBond.Enabled = true;
                l_addBond.Visible = true;
            }
            else if(actionActual==ACTION.DELETE)
            {
                bidder.idBidder = aBidder.idBidder;
                b_delete.Visible = true;
                b_delete.Enabled = true;
            }
            else if (actionActual == ACTION.MODIFY)
            {
                b_save.Visible = true;
                b_createVirtualAccount.Visible = true;
                this.ControlesHabilitados(true);
                pnlSearchClient.Visible = false;
            }
            else
            {
                b_createVirtualAccount.Visible = true;
                this.ControlesHabilitados(true);
                pnlSearchClient.Visible = false;
            }
        }

        private void LoadDataOfBidder(Bidder entityBidder)
        {
            if (entityBidder!=null)
            {
                t_Account.Text = entityBidder.Account;
                t_Adress.Text = entityBidder.Adress;
                t_Bond.Text = entityBidder.Bond.ToString();
                t_City.Text = entityBidder.City;
                t_Country.Text = entityBidder.Country;
                t_Name.Text = entityBidder.nameBidder;
                t_Number.Text = entityBidder.NumberAdress.ToString();
                t_Passport.Text = entityBidder.Passport;
                t_Surname1.Text = entityBidder.Surname1;
                t_Surname2.Text = entityBidder.Surname2;
                t_Telephone.Text = entityBidder.Telephone;
                t_Town.Text = entityBidder.Town;
                if (entityBidder.Sex=="Hombre")
                {
                    rBHombre.Checked = true;
                }
                else
                {
                    rB_Mujer.Checked = true;
                }
            }

   
        }

        private void b_save_Click(object sender, EventArgs e)
        {
            Output(DialogResult.OK);
            Close();
        }

        private void Output(DialogResult Salida)
        {
            switch (Salida)
            {
                case DialogResult.OK:

                    if (actionActual == ACTION.CREATE)
                    {
                        bidder = new Bidder();
                    }
                    if (rBHombre.Checked == true && rB_Mujer.Checked == false)
                    {
                        
                        bidder.Sex = "Hombre";
                    }
                    else if (rB_Mujer.Checked == true && rBHombre.Checked == false)
                    {
                        bidder.Sex = "Mujer";
                    }
                    
                    bidder.Telephone = t_Telephone.Text;
                   
                    bidder.Account = t_Account.Text.ToString();
                    bidder.Bond = valid.GetDecimal(t_Bond);
                    if (t_addBond.Visible)
                    {
                        bidder.Bond += valid.GetDecimal(t_addBond);
                    }
                    bidder.nameBidder = t_Name.Text.ToString();
                    bidder.Surname1 = t_Surname1.Text.ToString();
                    bidder.Surname2 = t_Surname2.Text.ToString();
                    bidder.Account = t_Account.Text.ToString();
                    bidder.Adress = t_Adress.Text.ToString();
                    bidder.City = t_City.Text.ToString();
                    bidder.Country = t_Country.Text.ToString();
                    bidder.NumberAdress = valid.GetByte(t_Number);
                    bidder.Passport = t_Passport.Text.ToString();
                    bidder.Sex = bidder.Sex;
                    bidder.Telephone = bidder.Telephone;
                    bidder.Town = t_Town.Text.ToString();
                   
                    if(actionActual==ACTION.CREATE)
                    {
                        context.BidderSet.AddObject(bidder);
                        context.SaveChanges();
                    }
                     else if (actionActual == ACTION.MODIFY)
                    {
                        context.BidderSet.ApplyCurrentValues(bidder);
                        context.SaveChanges();
                    }
                    MessageBox.Show("Bidder saved succesfully", "New client", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ((FormMainMenu)(this.ParentForm)).CloseAll();
                    break;

                case DialogResult.Cancel: MessageBox.Show("The client has not been saved", "Advice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;

                case DialogResult.Retry:

                    context.BidderSet.DeleteObject(bidder);
                    context.SaveChanges();
                    MessageBox.Show("The client has been deleted", "Client Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;                
            }
        }

        private void b_exit_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Cancel);
            Close();
        }

        private void pnlSearchClient_Paint(object sender, PaintEventArgs e)
        {

        }

        private void b_createVirtualAccount_Click(object sender, EventArgs e)
        {
            proposeAccountNumber();
        }

        private void  proposeAccountNumber()
        {
           Random rand=new Random();
            string bank = rand.Next(1000, 9999).ToString();
            string agency = rand.Next(1000, 9999).ToString();
            string DC = rand.Next(10, 99).ToString(); ;
            string account = rand.Next(10000, 99999).ToString() + rand.Next(10000, 99999).ToString();
            t_Account.Text = bank + agency + DC + account;
        }

        private void b_delete_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Retry);
            Close();
        }

     
    }
}
